package qq2564874169.tuaotuao.fx.utils;


import qq2564874169.tuaotuao.fx.FxAssert;
import qq2564874169.tuaotuao.fx.FxEnv;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Pattern;

public class ResUtil {
    public static String getAbsolutePath(String path) {
        File file = new File(path);
        if (file.exists()) {
            return file.getAbsolutePath();
        }
        if (FxEnv.jarPath() != null) {
            URL url = ResUtil.class.getResource(path);
            if (url != null) {
                return url.toString();
            }
            if (path.startsWith("/") == false) {
                url = ResUtil.class.getResource("/" + path);
                if (url != null) {
                    return url.toString();
                }
            }
        }
        return path;
    }

    public static InputStream getStream(String path) {
        return getStream(path, FxEnv.getMainClass() != null ? FxEnv.getMainClass() : ClassLoader.class);
    }

    public static InputStream getStream(String path, Class<?> root) {
        if (root == null) {
            return getStream(path);
        }
        path = StringUtil.trimLeft(path, "/");
        InputStream stream = null;
        if (new File(path).exists()) {
            try {
                stream = new FileInputStream(path);
            } catch (FileNotFoundException ignored) {

            }
        }
        if (stream == null && new File("/" + path).exists()) {
            try {
                stream = new FileInputStream("/" + path);
            } catch (FileNotFoundException ignored) {

            }
        }
        if (stream == null) {
            stream = root.getResourceAsStream(path);
        }
        if (stream == null) {
            stream = root.getResourceAsStream("/" + path);
        }
        return stream;
    }

    public static List<String> getFilesFromDir(String dir) {
        List<String> list = new ArrayList<>();
        dir = dir.replace("/", FxEnv.fileSeparator());
        File ioFile = new File(dir);
        if (ioFile.isDirectory() && ioFile.exists()) {
            File[] files = ioFile.listFiles();
            if (files != null) {
                for (File item : files) {
                    list.add(item.getPath());
                }
            }
        }
        return list;
    }

    public static List<String> getFilesFromJar(String jarPath, String dir) {
        jarPath = StringUtil.trimLeft(jarPath, "file:", true);
        List<String> list = new ArrayList<>();
        File file = new File(jarPath);
        FxAssert.isTrue(file.exists(), "文件不存在");
        dir = dir.toLowerCase().replace(FxEnv.fileSeparator(), "/");
        if (dir.startsWith("/")) {
            dir = dir.substring(1);
        }
        boolean findAll = false;
        if (dir.endsWith("/*")) {
            findAll = true;
            dir = dir.substring(0, dir.length() - 1);
        } else if (dir.endsWith("/") == false) {
            dir += "/";
        }
        Pattern allPat = Pattern.compile("^" + dir + ".+", Pattern.CASE_INSENSITIVE);
        Pattern dirPat = Pattern.compile("^" + dir + ".+?/", Pattern.CASE_INSENSITIVE);
        try (JarFile jarFile = new JarFile(file)) {
            Enumeration<JarEntry> entries = jarFile.entries();
            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                if (allPat.matcher(entry.getName()).find()) {
                    if (findAll == false && dirPat.matcher(entry.getName()).find()) {
                        continue;
                    }
                    list.add(entry.getName());
                }
            }
        } catch (IOException e) {
            throw ExUtil.toRuntime(e);
        }
        return list;
    }
}
